using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LoginSessionController : ControllerBase
    {
        private readonly ILoginSessionService _sessionService;

        public LoginSessionController(ILoginSessionService sessionService)
        {
            _sessionService = sessionService;
        }

        [HttpGet]
        public async Task<ActionResult<List<LoginSession>>> GetAllSessions()
        {
            var sessions = await _sessionService.GetAllAsync();
            return Ok(sessions);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<LoginSession>> GetSessionById(Guid id)
        {
            var session = await _sessionService.GetByIdAsync(id);
            if (session == null)
                return NotFound();

            return Ok(session);
        }

        [HttpGet("token/{token}")]
        public async Task<ActionResult<LoginSession>> GetSessionByToken(string token)
        {
            var session = await _sessionService.GetByTokenAsync(token);
            if (session == null)
                return NotFound();

            return Ok(session);
        }

        [HttpGet("user/{userId}/active")]
        public async Task<ActionResult<List<LoginSession>>> GetActiveSessionsByUserId(Guid userId)
        {
            var sessions = await _sessionService.GetActiveSessionsByUserIdAsync(userId);
            return Ok(sessions);
        }

        [HttpGet("validate/{token}")]
        public async Task<ActionResult<bool>> ValidateSession(string token)
        {
            var isValid = await _sessionService.IsSessionValidAsync(token);
            return Ok(isValid);
        }

        [HttpPost]
        public async Task<ActionResult<LoginSession>> CreateSession(LoginSession session)
        {
            var createdSession = await _sessionService.CreateAsync(session);
            return CreatedAtAction(nameof(GetSessionById), new { id = createdSession.SessionId }, createdSession);
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<LoginSession>> UpdateSession(Guid id, LoginSession session)
        {
            if (id != session.SessionId)
                return BadRequest();

            try
            {
                var updatedSession = await _sessionService.UpdateAsync(session);
                return Ok(updatedSession);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPost("{id}/deactivate")]
        public async Task<ActionResult> DeactivateSession(Guid id)
        {
            var result = await _sessionService.DeactivateSessionAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }

        [HttpPost("user/{userId}/deactivate-all")]
        public async Task<ActionResult> DeactivateAllUserSessions(Guid userId)
        {
            await _sessionService.DeactivateAllUserSessionsAsync(userId);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteSession(Guid id)
        {
            var result = await _sessionService.DeleteAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }
    }
} 